package com.mhc.haval.service.impl;

import com.mhc.haval.api.dto.StaffDTO;
import com.mhc.haval.api.service.IStaffInfoService;
import com.mhc.haval.bo.DepartmentBO;
import com.mhc.haval.bo.EditPasswordBO;
import com.mhc.haval.bo.StaffBO;
import com.mhc.haval.constant.AreaEnum;
import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.dal.mapper.ext.RoleExtMapper;
import com.mhc.haval.dal.mapper.ext.StaffAreaExtMapper;
import com.mhc.haval.dal.mapper.ext.StaffExtMapper;
import com.mhc.haval.dal.model.DepartmentDO;
import com.mhc.haval.dal.model.RoleDO;
import com.mhc.haval.dal.model.StaffAreaDO;
import com.mhc.haval.dal.model.StaffDO;
import com.mhc.haval.dal.query.DepartmentQuery;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.manager.DepartmentManager;
import com.mhc.haval.manager.StaffManager;
import com.subaru.common.util.BeanCopierUtil;
import com.subaru.common.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by HMJ on 2017/10/12.
 */
@Slf4j
@RestController
@Transactional(rollbackFor = Exception.class)
public class StaffInfoServiceImpl implements IStaffInfoService {

    @Autowired
    private StaffExtMapper staffExtMapper;
    @Autowired
    private RoleExtMapper roleExtMapper;
    @Autowired
    private StaffAreaExtMapper staffAreaExtMapper;
    @Autowired
    private StaffManager staffManager;
    @Autowired
    DepartmentManager departmentManager;
    /**
     * 查看员工详情信息
     *
     * @param staffId
     * @return
     */
    @Override
    public StaffDTO queryStaffInfoByStaffId(Long staffId) throws HavalCommonException {
        log.info("开始查询员工详情...");
        /**组装查询信息**/
        Map<String, Object> paramMap = new HashMap<>(64);
        paramMap.put("staffId", staffId);                //员工ID
        /**查询员工详情**/
        List<StaffBO> dataList = staffExtMapper.queryByStaffList(paramMap);
        StaffBO staffBO = dataList.get(0);
        List<RoleDO> list = roleExtMapper.queryByUserid(staffBO.getStaffId());
        staffBO.setRoleArray(rolesToStr(list));
        Map<String, Object> staffAreamMap = new HashMap<>(64);
        staffAreamMap.put("staffId", staffId);
        staffAreamMap.put("areaType", AreaEnum.BANK.getCode());
        StaffAreaDO staffAreaList = staffAreaExtMapper.selectByStaffArea(staffAreamMap);
        if (staffAreaList != null) {
            staffBO.setBankId(staffAreaList.getAreaId());
        }
        Long deptId = staffBO.getDeptId();
        TreeMap<Integer, Object> deptStructure = new TreeMap<>();
        for (int i=0;deptId!=null;i++) {
            //查询当前部门的详情
            DepartmentQuery thisDepartmentQuery = new DepartmentQuery();
            thisDepartmentQuery.createCriteria().andDeptIdEqualTo(deptId);
            List<DepartmentDO>  departList = departmentManager.selectByQuery(thisDepartmentQuery);
            if(!departList.isEmpty()){
                DepartmentDO departmentDO = departList.get(0);
                DepartmentBO departmentBO = new DepartmentBO();
                BeanCopierUtil.copy(departmentDO,departmentBO);
                //放入部门结构map中
                deptStructure.put(i, departmentBO);
                //继续递归往上查
                deptId = departmentDO.getParentDeptId();
            }else {
                deptId = null;
            }
        }
        staffBO.setDeptStructure(deptStructure.descendingMap());
        StaffDTO staffDTO = new StaffDTO();
        BeanCopierUtil.copy(staffBO, staffDTO);
        return staffDTO;
    }

    /**
     * 修改密码
     * @param editPasswordBO 修改密码实体
     * @return
     */
    @Override
    public Boolean updateByPassword(EditPasswordBO editPasswordBO) throws HavalCommonException{
        log.info("开始修改密码...");
        Long staffId = editPasswordBO.getStaffId();     //员工编号
        String oldPassword = editPasswordBO.getOldPassword();  //旧密码
        String newPassword = editPasswordBO.getNewPassword();  //新密码
        /**根据员工ID查询员工信息**/
        if(oldPassword!=null && newPassword!=null){
            StaffDO staffInfo = staffManager.selectByPrimaryKey(staffId);
            String staffPassword = staffInfo.getPassword();   //数据库的密码  加密的
            String md5OldPassword = MD5Util.stringMD5(oldPassword);  //旧密码加密后
            /**验证页面密码与原密码是否相同**/
            log.info("正在验证密码是否相同...");
            if (md5OldPassword.endsWith(staffPassword)) {
                /**旧密码加密后与数据库相同**/
                log.info("密码相同，开始修改密码...");
                String md5NewPassword = MD5Util.stringMD5(newPassword);  //页面上新密码加密
                if(md5NewPassword.endsWith(md5OldPassword)){
                    throw new HavalCommonException(MsgCodeEnum.PASSWORD_ALIKE);  //新密码与原密码相同
                }else {
                    StaffDO record = new StaffDO();
                    record.setStaffId(staffId);
                    record.setPassword(md5NewPassword);
                    staffManager.updateByPrimaryKeySelective(record);
                    log.info("修改完成...");
                    return true;
                }
            }
            /**原密码与数据库不同**/
            throw new HavalCommonException(MsgCodeEnum.PASSWORD_ERROR);  //密码验证失败
        }
        throw new HavalCommonException(MsgCodeEnum.PARAM_ERROR);  //参数错误
    }

    /**
     * 角色列表转成字符串
     *
     * @param list
     * @return
     */
    private List<Map> rolesToStr(List<RoleDO> list) throws HavalCommonException{
        List<Map> arrylist = new ArrayList<>();
        if(list!=null && !list.isEmpty()){
            /**循环遍历list取出角色ID和角色名称**/
            for (int i = 0; i < list.size(); i++) {
                RoleDO role = list.get(i);
                Map<String, Object> paramMap = new HashMap<>(64);
                paramMap.put("roleId", role.getRoleId());
                paramMap.put("roleName", role.getRoleName());
                arrylist.add(paramMap);
            }
        }
        return arrylist;
    }
}
